package com.shop.controller;

import com.alibaba.fastjson.JSON;
import com.shop.entity.Address;
import com.shop.entity.Order;
import com.shop.entity.User;
import com.shop.servlet.impl.AddressServletImpl;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.util.List;

/**
 * com.shop.controller文档说明
 *
 * @author Angluzha
 * @version 2022/9/22
 */
@WebServlet("/address/*")
public class AddressController extends BaseController<Address>{
    {
        servlet = new AddressServletImpl();
    }

    public String pay(){
        try {
            String line = req.getReader().readLine();
            List<Order> orders = JSON.parseArray(line, Order.class);

            HttpSession session = req.getSession();
            Integer id = ((User)session.getAttribute("user")).getId();
            orders.forEach(e->e.setUser(id));
            BigDecimal sum = new BigDecimal(0);
            for (Order order:orders) {
                sum = sum.add(order.getSpend());
            }

            AddressServletImpl servlet = (AddressServletImpl) this.servlet;
            List<Address> addressList = servlet.getListByUid(id);
            session.setAttribute("sum",sum);
            session.setAttribute("orders",orders);
            session.setAttribute("addressList",addressList);

            return writeWeb("yes");
        } catch (Exception e) {
            e.printStackTrace();
            return writeWeb("no");
        }
    }
}
